package zpx.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.compass.core.util.StringUtils;
import org.springframework.stereotype.Service;

import zpx.idao.IMenuDao;
import zpx.iservice.IMenuService;
import zpx.model.Material;
import zpx.model.Menu;
import zpx.model.Pager;
import zpx.model.TableUtil;
import zpx.model.User;
import zpx.model.UserException;

@Service("menuService")
public class MenuService implements IMenuService{
	private IMenuDao menuDao;
	
	public IMenuDao getMenuDao() {
		return menuDao;
	}
	
	@Resource
	public void setMenuDao(IMenuDao menuDao) {
		this.menuDao = menuDao;
	}

	@Override
	public void add(Menu menu) {
		Menu m = menuDao.loadByMenuname(menu.getName());
		if(m!=null) throw new UserException("�Ѵ�������˵�");
		menuDao.add(menu);
		
	}

	@Override
	public void update(Menu menu) {
		menuDao.update(menu);
		
	}

	@Override
	public void delete(String id) {
		menuDao.delete(id);
		
	}

	@Override
	public Menu load(String id) {
		return menuDao.load(id);
	}

	@Override
	public List<Menu> list() {
		return menuDao.list();
	}

	@Override
	public Pager<Menu> find() {
		return menuDao.find();
	}

	@Override
	public List<Menu> pMenulist() {
		return menuDao.pMenulist();
	}

	@Override
	public List<Menu> cMenulist(String id) {
		return menuDao.cMenulist(id);
	}

	@Override
	public String saveOrUpdate(Map<String, Object> map) {
		Menu menu=(Menu) map.get("menu");
		User loginUser=(User) map.get("loginUser");
		
		//修改
		if(StringUtils.hasText(menu.getId())){
			menu.setUpdateDate(new Date());
			menu.setUpdatePerson(loginUser.getId());
			this.update(menu);
		}else{
			//添加
	        String attaId = TableUtil.getPK("MA");
	        menu.setId(attaId);
			menu.setCreateDate(new Date());
			menu.setCreatePerson(loginUser.getId());
			this.add(menu);
		}
		return null;
	}

	

}
